<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            刀具使用登记
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">设备编码</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="扫描/录入设备编码" id="equipCode" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">产品编码</div>
                    <select id="mrlCode" onchange="mrlChange()">
                    </select>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">刀具序列号</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="扫描/录入序列号" id="toolSeqence" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
        </ul>
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info" style="margin-left:8%;width:40%" onclick="doSearch()">查询</div>
        <div class="aui-btn aui-btn-info" style="margin-left:5%;width:40%" onclick="save()">登记</div>
        <div style="background-color:white; height: 100%;width: 100%;margin-top:2%;margin-bottom:50px;">
            <table class="table" style='width:100%;' id="table">
                <thead>
                    <tr style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 14px;font-weight: bold;">
                        <td>序号</td>
                        <!-- <td algin='center'>设备编码</td> -->
                        <!-- <td algin='center'>产品编码</td> -->
                        <td algin='center'>刀具编码</td>
                        <td algin='center'>刀具序列号</td>
                        <td algin='center'>登记时间</td>
                    </tr>
                </thead>
                <tbody id="tbodyHtml">
                </tbody>
            </table>
        </div>
    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //初始化
    apiready = function() {

    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //查询产品下拉框
    function getCombox() {
        //取设备编码
        var equipCode = $("#equipCode").val();
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getComboxMrl";
        var data = {
            values: {
                equipCode: equipCode,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //成功
                if (myobj.code == "0") {

                    var list = eval(myobj.list);
                    var html = "<option value=''>--请选择--</option>";
                    for (var i = 0; i < list.length; i++) {
                        html += "<option value='" + list[i].code + "'>" + list[i].name + "</option>";
                    }

                    $api.html($api.byId("mrlCode"), html);

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //产品编码值改变
    function mrlChange() {

        //设备编码
        var equipCode = $("#equipCode").val();
        //产品编码
        var mrlCode = $("#mrlCode").val();

        //设备编码非空校验
        if (equipCode == null || equipCode == "" || equipCode == undefined) {
            return;
        }
        //产品编码非空校验
        if (mrlCode == null || mrlCode == "" || mrlCode == undefined) {
            return;
        }

        doSearch();
    }

    //查询刀具BOM（设备产品需要哪些刀具）
    function doSearch() {

        //设备编码
        var equipCode = $("#equipCode").val();
        //产品编码
        var mrlCode = $("#mrlCode").val();

        //设备编码非空校验
        if (equipCode == null || equipCode == "" || equipCode == undefined) {
            api.toast({
                msg: "请录入或扫描设备编码",
                duration: 3000
            });
            return;
        }
        //产品编码非空校验
        if (mrlCode == null || mrlCode == "" || mrlCode == undefined) {
            api.toast({
                msg: "请选择产品编码",
                duration: 3000
            });
            return;
        }
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/doSearchToolBom";
        var data = {
            values: {
                equipCode: equipCode,
                mrlCode: mrlCode,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //成功
                if (myobj.code == "0") {

                    var list = eval(myobj.list);
                    var html = "";
                    for (var i = 0; i < list.length; i++) {

                        //只可以修改未退货的数据
                        html += "<tr style='text-align: center;background-color: #F5F5F5;color: black;font-size: 11px;'>";
                        html += "<td name='seqno'>" + (i + 1) + "</td>";
                        // html += "<td name='equipCode'>" + list[i].equipCode + "</td>";
                        // html += "<td name='mrlCode'>" + list[i].mrlCode + "</td>";
                        html += "<td name='toolMrlCode'>" + list[i].toolMrlCode + "</td>";
                        html += "<td name='toolSeqence'>" + list[i].toolSeqence + "</td>";
                        html += "<td name='createDate'>" + list[i].createDate + "</td>";
                        html += "</tr>";
                    }
                    $api.html($api.byId("tbodyHtml"), html);

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //刀具登记
    function save() {

        //设备编码
        var equipCode = $("#equipCode").val();
        //产品编码
        var mrlCode = $("#mrlCode").val();
        //刀具唯一号
        var toolSeqence = $("#toolSeqence").val();

        //设备编码非空校验
        if (equipCode == null || equipCode == "" || equipCode == undefined) {
            api.toast({
                msg: "请录入或扫描设备编码",
                duration: 3000
            });
            return;
        }
        //产品编码非空校验
        if (mrlCode == null || mrlCode == "" || mrlCode == undefined) {
            api.toast({
                msg: "请选择产品编码",
                duration: 3000
            });
            return;
        }

        //产品编码非空校验
        if (toolSeqence == null || toolSeqence == "" || toolSeqence == undefined) {
            api.toast({
                msg: "请录入或扫描刀具序列号",
                duration: 3000
            });
            return;
        }
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/toolRegisterSave";
        var data = {
            values: {
                equipCode: equipCode,
                mrlCode: mrlCode,
                toolSeqence: toolSeqence,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {

                api.hideProgress();
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //返回成功
                if (myobj.code == "0") {

                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });

                    $("#toolSeqence").val("");

                    //查询刀具BOM信息
                    doSearch();

                    //失败，存在没有订单数据
                } else if (myobj.code == "1") {

                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {

                api.hideProgress();
                alert(JSON.stringify(err));
            }
        });
    }

    //自动保存
    function saveAuto() {

        //设备编码
        var equipCode = $("#equipCode").val();
        //产品编码
        var mrlCode = $("#mrlCode").val();
        //刀具序列号
        var toolSeqence = $("#toolSeqence").val();

        //设备编码非空校验
        if (equipCode == null || equipCode == "" || equipCode == undefined) {
            return;
        }

        //物料号非空校验
        if (mrlCode == null || mrlCode == "" || mrlCode == undefined) {
            return;
        }

        //刀具序列号非空校验
        if (toolSeqence == null || toolSeqence == "" || toolSeqence == undefined) {
            return;
        }

        save();
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function(ret, err) {

            $("#" + idSelector).val(ret);
            //1：设备扫描
            if (idSelector == "equipCode") {

                //查询产品下拉框
                getCombox();

                //2：刀具序列号
            } else if (idSelector == "toolSeqence") {

                saveAuto();

            }
        });
    }

    $("#equipCode").on('keypress', function(e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {

            $("#equipCode").val(sm);

            //查询产品下拉框
            getCombox();
        }
    });

    $("#toolSeqence").on('keypress', function(e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {

            $("#toolSeqence").val(sm);

            saveAuto();
        }
    });

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>
